home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / program / srcbkvt.zip / AWKPB.ASC < prev    next >
Text File  |  1996-07-08  |  4KB  |  163 lines

  1. _Adding Power to PowerBuilder_
  2. by Baylor Wetzel
  3.  
  4. Listing One
  5. Protected:
  6. int     ii_file_handle  // File Handle
  7. int     ii_num_fields   // Equiv of $nf
  8. long    il_num_lines    // Equiv of $nr
  9. string  is_current_line // Equiv of $0
  10. string  isa_fields[]    // Fields
  11.  
  12. Public:
  13. string  is_file_name
  14. int     ii_error_status
  15.  
  16. /* Error Return Code Constants - 5.0 only */
  17. constant int    NO_ERROR            =  0
  18. constant int    NO_FILE             = -1
  19. constant int    FILE_NOT_EXIST      = -2
  20. constant int    CANNOT_OPEN_FILE    = -3
  21. constant int    FILE_READ_ERROR     = -4
  22. constant int    INVALID_FIELD       = -5
  23.  
  24.  
  25. Listing Two
  26. public string field( int ai_field )
  27. public int nf( )
  28. public long nr( )
  29. protected parse_file( )
  30. public int parse_line( string as_line )
  31. public start( )
  32.  
  33.  
  34. Listing Three
  35. /* Make sure they set the file name */
  36. IF ( is_file_name = "" ) THEN
  37.     ii_error_status = NO_FILE
  38.     TriggerEvent( "ue_error" )
  39.     return
  40. END IF
  41. /* Open the file, read only in line mode */
  42. ii_file_handle = FileOpen(is_file_name, LineMode!, Read!)
  43. IF ( ii_file_handle = -1 ) THEN
  44.     ii_error_status = CANNOT_OPEN_FILE
  45.     TriggerEvent( "ue_error" )
  46.     return
  47. END IF
  48. parse_file( )
  49.  
  50.  
  51. Listing Four
  52. /* FileRead return types */
  53. int     END_OF_FILE = -100
  54. int READ_ERROR  = -1
  55. int NO_CHAR     =  0
  56. int li_num_char_read
  57.  
  58. /* We haven't read anything yet, so set the number of lines to 0 */
  59.  
  60. il_num_lines = 0
  61.  
  62. /* Start in the Begin event */
  63. TriggerEvent( "ue_begin" )
  64.  
  65. /* Read through this until we hit the end of the file */
  66. DO
  67.     li_num_char_read = FileRead( ii_file_handle, is_current_line )
  68.     CHOOSE CASE li_num_char_read
  69.         CASE READ_ERROR
  70.             ii_error_status = FILE_READ_ERROR
  71.             TriggerEvent( "ue_error" )
  72.         CASE END_OF_FILE
  73.             // Nothing, the loop will terminate
  74.         CASE ELSE
  75.             parse_line( is_current_line )
  76.             il_num_lines = il_num_lines + 1
  77.             TriggerEvent( "ue_scan" )
  78.     END CHOOSE
  79. LOOP WHILE ( li_num_char_read <> END_OF_FILE )
  80.  
  81. FileClose( ii_file_handle )
  82. TriggerEvent( "ue_end" )
  83.  
  84.  
  85. Listing Five
  86. int NO_MORE_DELIMITERS = 0  // We've hit the end of the line
  87. long ll_pos         // position of the delimiter within the string
  88.  
  89. /* We are just starting, so set the number of fields to 0 */
  90. ii_num_fields = 0
  91.  
  92. /* Get the string ready */
  93. as_line = Trim( as_line )
  94.  
  95. /* Figure out where the first delimiter is */
  96. ll_pos = Pos( as_line, " " )
  97.  
  98. /* Parse until position is 0 (which means 1 more record exists) */
  99. DO WHILE ( ll_pos <> NO_MORE_DELIMITERS )
  100.     /* We've found one, so increase our field counter */
  101.     ii_num_fields = ii_num_fields + 1
  102.  
  103.     /* Store the field */
  104.     isa_fields[ii_num_fields] = Trim( Left( as_line, ll_pos))
  105.  
  106.     /* Remove that part from our string */
  107.     as_line = Trim( mid( as_line, ll_pos ))
  108.     /* And find the next delimiter - if 0, allow one more go-round */
  109.     ll_pos = Pos( as_line, " ")
  110. LOOP
  111. /* One more record may be left, and if so, store it */
  112. IF ( as_line <> "" ) THEN
  113.     ii_num_fields = ii_num_fields + 1
  114.     isa_fields[ii_num_fields] = Trim( as_line )
  115.  
  116. END IF
  117. return ii_num_fields
  118.  
  119.  
  120. Listing Six
  121. public string field( int ai_field ):
  122. CHOOSE CASE ai_field
  123.     CASE 0
  124.         return is_current_line
  125.     CASE 1 to nf( )
  126.         return isa_fields [ ai_field ]
  127.     CASE ELSE
  128.         ii_error_status = INVALID_FIELD
  129.         TriggerEvent( "ue_error" )
  130.         return ""
  131. END CHOOSE
  132.  
  133. public int nf( ):
  134. return ii_num_fields
  135.  
  136. public long nr( ):
  137. return il_num_lines
  138.  
  139.  
  140. Listing Seven
  141. int     i
  142. uo_nv_awk   luo_awk
  143. luo_awk = CREATE uo_nv_awk
  144.  
  145. luo_awk.parse_line( 'Hello World' )
  146. FOR I = 1 TO luo_awk.nf( )
  147.     messagebox( 'test', 'field '+string(i)+' is '+luo_awk.field( i ) )
  148. NEXT
  149.  
  150. DESTROY iuo_nv_awk
  151.  
  152.  
  153. ??
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.